// i.d(t, { f: () => ObservableValue, Y: () => r })
import Observable, { ChangeObserver } from "./observable"
import funcUtil from "../utils/func.util"

export default class ObservableValue<T = any> extends Observable<T> {
  private _value: T
  constructor(intialValue: T) {
    super()
    this.value = intialValue
  }
  notifyObserver(observer: ChangeObserver<T>) {
    observer(this.value)
  }
  get value() {
    return this._value
  }
  set value(newVal: T) {
    if (this._value !== newVal) {
      this.removeChildObservables(this._value)
      this.addChildObservable(newVal)
      this._value = newVal
      this.setDirty()
    }
  }
  deepCopy() {
    return funcUtil.deepCopy(this._value)
  }
}
export const createObVal = <T>(value: T) => new ObservableValue(value)
